package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.AlbumItemVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {
    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Resource
    private CategoryFeignClient categoryFeignClient;
    @Resource
    private UserInfoFeignClient userInfoFeignClient;
    @Resource
    private ExecutorService executorService;


    @Override
    public AlbumItemVo loadItemData(Long albumId) {
        AlbumItemVo albumItemVo = new AlbumItemVo();

        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoResult, "获取专辑信息失败");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑信息不存在");
            albumItemVo.setAlbumInfo(albumInfo);
            return albumInfo;
        }, executorService);

        CompletableFuture<Void> baseCategoryFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(baseCategoryViewResult, "获取三级分类失败");
            BaseCategoryView category3View = baseCategoryViewResult.getData();
            albumItemVo.setBaseCategoryView(category3View);
        }, executorService);

        CompletableFuture<Void> albumStatFuture = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> albumStatByAlbumIdResult = albumInfoFeignClient.getAlbumStatByAlbumId(albumId);
            Assert.notNull(albumStatByAlbumIdResult, "获取专辑统计信息失败");
            AlbumStatVo albumStat = albumStatByAlbumIdResult.getData();
            albumItemVo.setAlbumStatVo(albumStat);
        }, executorService);

        CompletableFuture<Void> userInfoFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfo> userInfoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(userInfoResult, "获取主播信息失败");
            UserInfo userInfo = userInfoResult.getData();
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo, userInfoVo);
            albumItemVo.setAnnouncer(userInfoVo);
        }, executorService);
        CompletableFuture.allOf(baseCategoryFuture,albumStatFuture,userInfoFuture).join();

        return albumItemVo;
    }
}
